<?php echo $header; ?>

<?php if ($error_warning) { ?>
    <div class="warning"><?php echo $error_warning; ?></div>
<?php } ?>

<?php if ($success) { ?>
    <div class="success"><?php echo $success; ?></div>
<?php } ?>

<div class="box">
    <div class="left"></div>
    <div class="right"></div>
    <div class="heading">
        <h1 style="background-image: url('view/image/product.png');"><?php echo $heading_title; ?></h1>
        <div class="buttons">
            <a onclick="processSubmit();" class="button"><span>Save</span></a>            
        </div>
    </div>
    <div class="content">
        <form action="<?php echo $save; ?>" method="post" enctype="multipart/form-data" id="form">
            <table class="list">
                <thead>
                    <tr>
                        <td width="1" style="text-align: center;"><input type="checkbox" onclick="$('input[name*=\'selected\']').attr('checked', this.checked);" /></td>
                        <td class="center"><?php echo $column_image; ?></td>
                        <td class="left">
                            <?php if ($sort == 'pd.name') { ?>
                                <a href="<?php echo $sort_name; ?>" class="<?php echo strtolower($order); ?>"><?php echo $column_name; ?></a>
                            <?php } else { ?>
                                <a href="<?php echo $sort_name; ?>"><?php echo $column_name; ?></a>
                            <?php } ?>
                        </td>                                                  
                        <td class="left">
                            <?php if ($sort == 'p.sku') { ?>
                                <a href="<?php echo $sort_sku; ?>" class="<?php echo strtolower($order); ?>"><?php echo $column_sku; ?></a>
                            <?php } else { ?>
                                <a href="<?php echo $sort_sku; ?>"><?php echo $column_sku; ?></a>
                            <?php } ?>
                        </td>   
                        <td class="left">
                            <?php if ($sort == 'pms.product_msort_order') { ?>
                                <a href="<?php echo $sort_msort_order; ?>" class="<?php echo strtolower($order); ?>">Manufacturer Sort</a>
                            <?php } else { ?>
                                <a href="<?php echo $sort_msort_order; ?>">Manufacturer Sort</a>
                            <?php } ?>                      
                        </td>
                        <td class="left">
                            <?php if ($sort == 'manufacturer') { ?>
                                <a href="<?php echo $sort_manufacturer; ?>" class="<?php echo strtolower($order); ?>">Manufacturer</a>
                            <?php } else { ?>
                                <a href="<?php echo $sort_manufacturer; ?>">Manufacturer</a>
                            <?php } ?>
                        </td> 
                        <td class="left">
                            <?php if ($sort == 'category') { ?>
                                <a href="<?php echo $sort_category; ?>" class="<?php echo strtolower($order); ?>"><?php echo $column_category; ?></a>
                            <?php } else { ?>
                                <a href="<?php echo $sort_category; ?>"><?php echo $column_category; ?></a>
                            <?php } ?>
                        </td>                         
                        <!-- td class="right"><?php //echo $column_action; ?></td -->
                    </tr>
                </thead>
                <tbody>
                    <tr class="filter">
                        <td></td>
                        <td></td>
                        <td><input type="text" name="filter_name" value="<?php echo $filter_name; ?>" /></td>                                                
                        <td><input type="text" name="filter_sku" value="<?php echo $filter_sku; ?>" size="14" /></td>            
                        <td></td>
                        <td style="width:220px;">
                            <select name="filter_manufacturer" style="width:140px;">
                                <option value="*"></option>
                                <?php foreach ($manufacturers as $manufacturer) { ?>
                                    <?php $selected = $manufacturer['manufacturer_id'] == $filter_manufacturer ? 'selected="selected"' : ''; ?>
                                        <option value="<?php echo $manufacturer['manufacturer_id']; ?>" <?php echo $selected; ?>><?php echo $manufacturer['name']; ?></option>                        
                                <?php } ?>
                            </select>                            
                        </td>  
                        <td style="width:300px;">
                            <select name="filter_category" style="width:200px;">
                                <option value="*"></option>
                                <?php foreach ($categories as $category) { ?>
                                    <?php if ($category['category_id']==$filter_category) { ?>
                                        <option value="<?php echo $category['category_id']; ?>" selected="selected"><?php echo $category['name']; ?></option>
                                    <?php } else { ?>
                                        <option value="<?php echo $category['category_id']; ?>"><?php echo $category['name']; ?></option> 
                                    <?php } ?>
                                <?php } ?>
                            </select>&nbsp;&nbsp;&nbsp;
                            <a onclick="filter();" class="button" style="margin-left:10px;"><span><?php echo $button_filter; ?></span></a>
                        </td>                          
                        <!-- td align="right"><a onclick="filter();" class="button"><span><?php //echo $button_filter; ?></span></a></td -->
                    </tr>
                    <?php if ($products) { ?>
                        <?php $index = 0; ?>
                        <?php foreach ($products as $product) { ?>
                            <tr>
                                <td style="text-align: center;">
                                    <?php if ($product['selected']) { ?>
                                        <input type="checkbox" name="selected[]" value="<?php echo $product['product_id']; ?>" checked="checked" />
                                    <?php } else { ?>
                                        <input type="checkbox" name="selected[]" value="<?php echo $product['product_id']; ?>" />
                                    <?php } ?>
                                </td>
                                <td class="center"><img src="<?php echo $product['image']; ?>" alt="<?php echo $product['name']; ?>" style="padding: 1px; border: 1px solid #DDDDDD;" /></td>
                                <td class="left"><?php echo $product['name']; ?></td>                                                                 
                                <td class="left"><?php echo $product['sku']; ?></td> 
                                <td class="left"> 
                                    <?php if($product['manufacturer']){ ?> 
                                        <?php $style = ''; //($product['manufacturer']['manufacturer_id'] == $filter_manufacturer) ? ' style="margin:1px 0; width:50px; text-align:right; font-weight:bold;"' : ' style="margin:1px 0; width:50px; text-align:right;"'; ?>                                  
                                        <input type="hidden" name="sort[<?php echo $product['product_id']; ?>][sort_id]" value="<?php echo $product['sort_order_m']['msort_id']; ?>" />
                                        <input type="hidden" name="sort[<?php echo $product['product_id']; ?>][manufacturer_id]" value="<?php echo $product['manufacturer']['manufacturer_id']; ?>" />                                                                                        
                                        <input type="text" name="sort[<?php echo $product['product_id']; ?>][sort_order]" value="<?php echo $product['sort_order_m']['product_msort_order']; ?>" size="4"<?php echo $style; ?> /><br />
                                        <input type="hidden" name="sort[<?php echo $product['product_id']; ?>][sort_order_old]" value="<?php echo $product['sort_order_m']['product_msort_order']; ?>" />
                                    <?php } ?>
                                </td>
                                <td class="left"><?php echo (isset($product['manufacturer']['name']) ? $product['manufacturer']['name'] : ''); ?></td> 
                                <td class="left">
                                    <?php //echo (isset($product['category_name']) ? $product['category_name'] : ''); ?>                                    
                                    <input type="hidden" name="sort[<?php echo $product['product_id']; ?>][category_id]" value="<?php echo $product['sort_order_m']['category_id']; ?>" />                                        
                                    
                                    <?php
                                    foreach ($categories as $category) { 
                                        if($category['category_id'] == $filter_category) {
                                            echo $category['name'];
                                        }
                                        
                                        /*
                                        if(in_array($category['category_id'], $product['category'])){
                                            $style = ($category['category_id'] == $filter_category) ? 'style="padding:0; margin:6px 0; font-weight:bold;"' : 'style="padding:0; margin:6px 0;"';
                                            echo '<p ' . $style . '>' . $category['name'] . '</p>';
                                        }
                                        */                                        
                                    }
                                    ?>
                                </td>                                
                                <!-- td class="right">[ <a onclick="processSubmit();"><?php //echo $button_save; ?></a> ]</td -->
                            </tr>
                            <?php $index++; ?>
                        <?php } ?>
                    <?php } else { ?>
                        <tr>
                            <td class="center" colspan="9"><?php echo $text_no_results; ?></td>
                      </tr>
                    <?php } ?>
                </tbody>
            </table>
        </form>
        <div class="pagination"><?php echo $pagination; ?></div>
    </div>
</div>

<script type="text/javascript"><!--
function processSubmit(){
    var selected = $('input[name^=\'selected\']:checked');    
    
    if(selected.length <= 0){
        alert('Please select an item!');
        return;
    }
    
    $('#form').submit();
}

function filter() {
    url = 'index.php?route=catalog/product_sort/manufacturer';
    
    var filter_name = $('input[name=\'filter_name\']').attr('value');      
    if (filter_name) {
        url += '&filter_name=' + encodeURIComponent(filter_name);
    }
    
    var filter_model = $('input[name=\'filter_model\']').attr('value');    
    if (filter_model) {
        url += '&filter_model=' + encodeURIComponent(filter_model);
    }

    var filter_category = $('select[name=\'filter_category\']').attr('value');  
    if (filter_category != '*') {
        url += '&filter_category=' + encodeURIComponent(filter_category);
    }
    
    var filter_sku = $('input[name=\'filter_sku\']').attr('value');       
    if (filter_sku) {
        url += '&filter_sku=' + encodeURIComponent(filter_sku);
    }
    
    var filter_manufacturer = $('select[name=\'filter_manufacturer\']').attr('value');
    if (filter_manufacturer != '*') {
        url += '&filter_manufacturer=' + encodeURIComponent(filter_manufacturer);
    }
    
    location = url + '&token=<?php echo $token; ?>';
}

$('#form input').keydown(function(e) {
    if (e.keyCode == 13) {
        filter();
    }
});

$('#form select').change(function(e) {
    filter(); 
});
//--></script>

<?php echo $footer; ?>